var app = getApp();
var util = require('../../../../utils/util.js');
import {date,years,months,days,today} from '../../../../utils/date';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sear_value:'',
    page:1,
    datas: [],     //订单列表
    load:'加载中...',
    loading:1,  //正在加载
    check_status_list: ['待审批','已审批'],
    check_status: 0,   
    z_screen_type_index:-1,
    screen_type_index:-1,
    screen_type_list:['已同意','已驳回'],
    screen_area_list:['过去一天','过去一周','过去一月'],
    z_screen_area_index:-1,
    screen_area_index:-1,
    area_time:0, //时间段开始时间
    is_screen:false,  //筛选下拉
    z_choose_supplier_id:0,   //筛选供应商id
    z_choose_supplier_name:'',
    choose_supplier_id:0,  //筛选供应商id
    // 时间周期********
    years,
    year: date.getFullYear(),
    months:months,
    month: 2,
    days:days,
    day: 2,
    value: [date.getFullYear() - 2010, date.getMonth(), date.getDate() - 1],  //默认今天
    time_type: 0,  //时间time切换的初始索引值
    start_time: 0,  //开始时间
    today:today, //今天
    end_time:today,    //结束时间,默认今天
    z_start_time: '', //选择开始时间
    z_end_time:today,   //选择结束时间,默认今天
    c_start_time: 0,  //右窗开始时间
    c_end_time:today, //结束时间,默认今天
    // *****
    load_num:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.check_type=='check_order'){  //需求单审批
      wx.setNavigationBarTitle({
        title:"需求单审批"
      });
    }else if(options.check_type=='purchase_order'){  //采购单审批
      wx.setNavigationBarTitle({
        title:"采购单审批"
      });
    }else if(options.check_type=="receipt_order"){  //收货审批
      wx.setNavigationBarTitle({
        title:"收货审批"
      });
    };
    this.setData({
      loginInfo:app.globalData.loginInfo,
      use_person_id:app.globalData.person_id, //当前用户的个人id
      check_type:options.check_type,
      raplace_h:0.75*wx.getSystemInfoSync().windowHeight
    });
    this.updateList();
  },
  // 审批列表
  updateList: function () {
    var that = this;
    if(that.data.page==1){
      wx.showLoading({
        title: that.data.load
      });
    };
    var start_time;
    var select_time='';
    if(that.data.start_time==0){
      start_time=that.data.area_time;
      // new Date(that.data.area_time).getFullYear() + "-" + (new Date(that.data.area_time).getMonth() + 1) + "-" + new Date(that.data.area_time).getDate(); 
    }else{
      start_time=that.data.start_time;
    };
    var end_time =that.data.end_time;
    if(start_time!=0){
      select_time=start_time+' '+end_time
    };
    app.AppHttp('api/Order/getCheckList', {
      openid: app.globalData.openid,    
      check_type:that.data.check_type,
      status:that.data.check_status,
      page:that.data.page,
      keyword:that.data.sear_value,
      time:select_time
    }, function (res) {
      app.globalData.dingdan_show=0;
      app.globalData.caigou_shua=0;
      console.log(JSON.stringify(res.data));
      if(res.data.status!=-1){
        var datas;
        if (that.data.page == 1){
          datas = res.data.order_list;
        }else{
          datas = that.data.datas.concat(res.data.order_list);
        };
        that.setData({
          load_num:0,
          loading:0,
          datas: datas,
          count:res.data.count,
          page_count: res.data.page_count
        });
      };
      wx.hideLoading();
    })   
  },
  // 审批状态切换
  myselect:function(e){
    this.setData({
      check_status:e.currentTarget.dataset.index
    });
    app.globalData.task.abort();  
    this.updateList();
  },
  // 输入关键词
  input_sear:function(e){
    this.setData({
      sear_value:e.detail.value
    });
  },
  // 回车
  input_conf:function(){
    app.globalData.task.abort();  
    this.updateList();
  },
  noneEnoughPeople:function() {
    return;
  },
  // 点击进入详情
   to_check_detail: function(event) {
    var id = event.currentTarget.dataset.id;
    var index = event.currentTarget.dataset.index;
    let order_status = event.currentTarget.dataset.order_status;
    if(this.data.check_type=='check_order'){  //需求单审批
      var small_type=event.currentTarget.dataset.small_type;
      var charge_id=event.currentTarget.dataset.charge_id;
      var out_ku=order_status==2 && (this.data.loginInfo.is_manage==2 || this.data.use_person_id == charge_id)?1:0;  
      wx.navigateTo({
        url: '/pages/dingdan/need_detail/need_detail?id=' + id + "&order_status=" + order_status +'&small_type='+small_type+'&out_ku='+out_ku+"&d_index=" + index+'&small_type='+small_type+'&entPage='+1+"&d_index=" + index
      });      
    }else if(this.data.check_type=='purchase_order' || this.data.check_type=="receipt_order"){  //采购单and收货审批
      var btom_0=order_status>=6?0:7;
      var is_enquiry=event.currentTarget.dataset.is_enquiry;
      var is_created=event.currentTarget.dataset.is_created;
      var title_index=app.globalData.loginInfo.client_is_store<0 && app.globalData.type_level==1 || app.globalData.loginInfo.client_is_store==1?2:1;
      wx.navigateTo({
        url: '/pages/dingdan/order_detail/order_detail?id=' + id + "&order_status=" + order_status+ "&settitle=" + "采购单"+ "&title_index="+title_index+"&btom_0="+btom_0+"&is_enquiry="+is_enquiry+"&is_created="+is_created+'&entPage='+1+"&d_index=" + index
      });
    };
  },
  // 打开筛选下拉
  open_screen:function(){
    var is_screen=this.data.is_screen;
    if(is_screen){
      util.hideBuyModal(this,"is_screen");
    }else{
      util.showBuyModal(this,-1);
      this.setData({
        is_screen:true
       });
    } 
  },
  // 关闭筛选下拉
  close_screen:function(){
    util.hideBuyModal(this,"is_screen");
  },
  // 选择供应商
  chose_super:function(){
    var that = this;
    util.showBuyModal(this);  
    that.setData({
      issuper:true
    });
    app.AppHttp("api/Order/getCBelongSupplier", {
      openid: app.globalData.openid
    }, function(res) { 
      // var superlist=[{"supplier_id":"0","company_name":"全部"}];
      that.setData({
        // superlist:superlist.concat(res.data.data)
        superlist:res.data.data
      });
    });
  },
  // 确定供应商
  super_conf:function(e){
    var supplier_id=e.currentTarget.dataset.supplier_id;
    var company_name=e.currentTarget.dataset.company_name;
    this.setData({
      z_choose_supplier_id:supplier_id,
      z_choose_supplier_name:company_name
    });
    util.hideBuyModal(this,"issuper");
  },
  // 关闭供应商
  close_super:function(){
    util.hideBuyModal(this,"issuper");
  },
  // 选择筛选类型
  chose_screen_type:function(e){
    this.setData({
      z_screen_type_index:e.currentTarget.dataset.index
    });
  },
  // 选择时间段
  chose_area:function(e){
    this.setData({
      z_screen_area_index:e.currentTarget.dataset.index,
      c_start_time: '',
      c_end_time:today,
      z_start_time: '',
      z_end_time:today
    });
  },
  // 打开时间弹窗
  chose_time:function(e){
    util.showBuyModal(this); 
    if(this.data.z_start_time==''){
      this.setData({
        z_start_time:today
      });
    };
    this.setData({
      showModalStatus:true
    });
  },
  // 关闭时间弹窗
  hideBuyModal:function(){
    util.hideBuyModal(this,"showModalStatus");
  },
  // 时间周期选中效果
  bcolor: function (e) {
    var val_time=e.currentTarget.dataset.id==1?this.data.z_end_time:this.data.z_start_time;
    if(val_time!=''){
      val_time=(val_time).split('-');
      this.setData({
        value:[val_time[0] - 2010, val_time[1]-1, val_time[2] - 1]
      });
    }
    this.setData({
      time_type: e.currentTarget.dataset.id
    });
  },
  // 年、月、日改变
  bindChange: function (e) {
    util.bindchange_time(e,this,date)
  },
  // 重置时间
  reset_time_but:function(){
    if(this.data.z_start_time!='' || this.data.z_end_time!=today){
      this.setData({ 
        z_start_time: '', //选择开始时间
        z_end_time:today,   //选择结束时间,默认今天
      });
    };    
  },
  // 确认并关闭选择时间周期弹窗
  queding: function () {
    if (Date.parse(new Date(this.data.z_end_time)) < Date.parse(new Date(this.data.z_start_time))){
      wx.showLoading({
        title: '时间不合理',
        duration:1000
      });
    }else{
      util.hideBuyModal(this,"showModalStatus");
      this.setData({
        z_screen_area_index:-1,
        c_start_time: this.data.z_start_time,  //开始时间
        c_end_time: this.data.z_end_time,    //结束时间
      });
    }  
  },
  // 筛选查看
  screen_queding:function(){
    util.hideBuyModal(this,"is_screen");
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    });
    var area_time = 0;
    if(this.data.z_screen_area_index==0){  //过去1天
      area_time = Date.parse(new Date(today))-3600000 * 24
    }else if(this.data.z_screen_area_index==1){  //过去1周
      area_time = Date.parse(new Date(today))-3600000 * 24*7
    }else if(this.data.z_screen_area_index==2){  //过去30天
      area_time = Date.parse(new Date(today))-3600000 * 24*30
    };
    this.setData({
      choose_supplier_id:this.data.z_choose_supplier_id,
      choose_supplier_name:this.data.z_choose_supplier_name,
      screen_type_index:this.data.z_screen_type_index,
      screen_area_index:this.data.z_screen_area_index,
      area_time:area_time,
      start_time: this.data.c_start_time,
      end_time: this.data.c_end_time,
      loading:1,
      load:'加载中...',
      page:1
    })
    this.updateList();
  },
  // 全部重置
  screen_reset_but:function(){
    util.hideBuyModal(this,"is_screen");
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    });
    this.setData({
      z_choose_supplier_id:0,
      z_screen_type_index:-1,
      z_screen_area_index:-1,
      z_start_time: '',
      z_end_time:today,
      c_start_time: '',
      c_end_time:today,
      choose_supplier_id:0,
      choose_supplier_name:'',
      screen_type_index:-1,
      screen_area_index:-1,
      area_time:0,
      start_time:'',
      end_time:today,
      loading:1,
      load:'加载中...',
      page:1
    });
    this.updateList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(app.globalData.dingdan_show == 1 || app.globalData.caigou_shua==1){
      wx.pageScrollTo({
        scrollTop: 0,
        duration: 0
      });
      this.setData({
        page:1,
        load:'加载中...'
      });
      this.updateList();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    });
    this.setData({
      page:1,
      load:'加载中...'
    });
    this.updateList();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.page < this.data.page_count) {
      this.setData({
        page: this.data.page + 1,
        loading:1,
        load: '加载更多'
      });
      this.updateList();
    } else {
      this.setData({
        loading:0
      });
      wx.showToast({
        title: '没有了'
      });
    };
  }
})